#include <stdint.h>
#include <string.h>

#include <stdio.h>

#include "uch.h"

Ch_len
ch_len_f(const char *str)
{
	unsigned char c = *str;
	Ch_len len = 0;

	for (; c >> 7; ++len, c <<= 1);

	if (!len)
		return 1;

	return len;
}

Ch_len
ch_len_b(const char *str)
{
	Ch_len len = 1;

	--str;

	/* ascii, last bit not set */
	if (!(*str & (1 << 7)))
		return 1;

	/* not at start if second to last bit is not set */
	for (; !(*str & (1 << 6)); ++len, --str);

	return len;
}

Ch_len
ch_len(Uch ch)
{
	return ch_len_f((char *) &ch);
}


Uch
uch(const char *str, Ch_len *len)
{
	Uch ch = 0;

	*len = ch_len_f(str);
	memcpy(&ch, str, *len);
	return ch;
}
